package com.project.service.imp;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.bean.SupplierBean;
import com.project.mapper.ISupplierMapper;
import com.project.service.ISupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class SupplierServiceImp implements ISupplierService {
    @Autowired
    private ISupplierMapper supplierMapper;

    @Override
    public IPage<SupplierBean> cutByItem(Integer pageNO, String name, String scope) {
        QueryWrapper qw =new QueryWrapper<>();
        if(name!=null&& name.length()!=0) {
            qw.like("s_name",name);
        }
        if(scope!=null&& scope.length()!=0) {
            qw.like("s_scope",scope);
        }
        return supplierMapper.selectPage(new Page<>(pageNO,5),qw);
    }

    @Override
    public SupplierBean findById(Integer id) {
        return  supplierMapper.selectById(id);
    }

    @Override
    public void update(Integer id, String director, String phone) {
        SupplierBean supplierBean = supplierMapper.selectById(id);
        supplierBean.setDirector(director);
        supplierBean.setPhone(phone);
        supplierMapper.updateById(supplierBean);
    }

    @Override
    public SupplierBean findByName(String name) {
        QueryWrapper qw =new QueryWrapper<>();
        qw.eq("s_name",name);
        return supplierMapper.selectOne(qw);
    }

    @Override
    public void add(SupplierBean supplierBean) {
        supplierMapper.insert(supplierBean);
    }

    @Override
    public List<SupplierBean> findAll() {
        return supplierMapper.selectList(null);
    }
}
